﻿using HB.HttpServer.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace HB.HttpServer
{
    public class ApiController
    {
        public static Dictionary<string, ActionExecutingData> DicContent;
        public static void Clear(string key)
        {
            if (DicContent.ContainsKey(key))
            {
                var aed = DicContent[key];
                if (aed != null)
                {
                    aed.RequestBodyContent = null;
                    aed.Method = null;
                }
            }
            DicContent.Remove(key);
        }
        public class Request
        {
            public static dynamic UserData
            {
                get
                {
                    string Caller = Thread.CurrentPrincipal.Identity.Name;
                    if (DicContent.ContainsKey(Caller))
                    {
                        return DicContent[Caller].UserData;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            public static long ContentLength64
            {
                get
                {
                    string Caller = Thread.CurrentPrincipal.Identity.Name;
                    if (DicContent.ContainsKey(Caller))
                    {
                        return DicContent[Caller].HttpContex.Request.ContentLength64;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }

            public static string Authorization
            {
                get
                {
                    string Caller = Thread.CurrentPrincipal.Identity.Name;
                    if (DicContent.ContainsKey(Caller))
                    {
                        HttpListenerContext HC = DicContent[Caller].HttpContex;
                        return HC.Request.Headers.GetValues("Authorization")?[0]??"";
                    }
                    else
                    {
                        return "";
                    }
                }
            }
        }

      

        internal static object Exec(HttpListenerContext HC, Method M, List<dynamic> LsParamater)
        {
            return M.Methodinfo.Invoke(M.ControllerInstance, LsParamater.ToArray());
        }
    }
}
